//
//  DoctorDetailsViewController.m
//  QuickCure1
//
//  Created by summer的Dad on 16/10/31.
//  Copyright © 2016年 maoge. All rights reserved.
//
//系统版本号是否大于8.0
#define IS_SystemVersionGreaterThanEight  ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0)
#import "DoctorDetailsViewController.h"
#import "DoctorApplicationCell.h"
#import "DoctorDetailsView.h"
#import "DoctorDetailsButtonView.h"
#import "DoctorDetailTableViewCell.h"
#import "DoctorChatViewController.h"
#import "DoctorAddressTableViewCell.h"
#import <MapKit/MapKit.h>
#import "JZLocationConverter.h"


static NSString* CELL_CELLDATA = @"cell2";
static NSString* CELL_DETAILS = @"ce";
@interface DoctorDetailsViewController ()< DoctorAddressTableViewCellDelegate>
//中间cell
@property(nonatomic,weak)DoctorDetailTableViewCell* tableCell;



@property(nonatomic,weak)DoctorDetailsView* detailsView;
//底部视图
@property(nonatomic,weak)DoctorDetailsButtonView* buttonView;
//中间TableView
@property(nonatomic,weak)UITableView* listTableView;

//位置
@property (nonatomic,assign) CLLocationCoordinate2D destinationCoordinate2D;

//@property(nonatomic,strong)NSArray * maps;


@end

@implementation DoctorDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.detailsView.model = _model;
    
//    self.tableCell.model = self.model;
//    
//    NSLog(@"%@",self.tableCell.model);
    [self.tableView reloadData];
    
    //导航到
    
    self.destinationCoordinate2D = CLLocationCoordinate2DMake(1, 135);
    


}
#pragma mark
#pragma mark - 打开界面
- (void)setupUI
{
    NSString *str = @"审核";
    
    if ([MLDefaults boolForKey:_cell.model.doctor_portrait]) {
     
        str = @"审核中";
    }
    
    //顶部视图
    DoctorDetailsView * detailsView = [[DoctorDetailsView alloc] init];
    
    
    [self.view addSubview:detailsView];
    
    self.detailsView = detailsView;
    
    [detailsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(@100);
    }];

    
     detailsView.lb_str = str;
    
    
    //---------------------------底部两个按钮------------------------------------
    //解决内存泄漏问题
    __weak typeof(self) weaktype = self;
    DoctorDetailsButtonView* buttonView = [[DoctorDetailsButtonView alloc] init];
    
    buttonView.clickBlock = ^(NSInteger index)
    {
        if (index == 100)
        {
            [weaktype clickApply];
        }else
        {
            [weaktype clickCancel];
        }
    };
    
    self.buttonView = buttonView;
    [self.view addSubview:buttonView];
    [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.equalTo(@60);
    }];
    //--------------------------建立tableView--------------------------
   
    UITableView* tableView = [[UITableView alloc] initWithFrame:SCREEN_BOUNDS style:UITableViewStyleGrouped];
    self.tableView = tableView;
    // 自定义行高
//    [tableView setEstimatedRowHeight:100];
//    [tableView setRowHeight:UITableViewAutomaticDimension];
//
    tableView.rowHeight = 100;
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.listTableView = tableView;
    //注册
    [tableView registerClass:[DoctorDetailTableViewCell class] forCellReuseIdentifier:CELL_DETAILS];
    [tableView registerClass:[DoctorAddressTableViewCell class] forCellReuseIdentifier:CELL_CELLDATA];
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.detailsView.mas_bottom);
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.buttonView.mas_top);
    }];
  
}


#pragma mark
#pragma mark - 通知监听方法
//重新加载
-(void)clickApply{
    
    
    [MLDefaults setBool:YES forKey:_cell.model.doctor_portrait];
    
    self.detailsView.lb_str = @"审核中";
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
//取消
- (void)clickCancel
{
    
    
    [MLDefaults setBool:NO forKey:_cell.model.doctor_portrait];
    

    self.detailsView.lb_str = @"审核";
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark
#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 && indexPath.row == 0)
    {
    
    DoctorDetailTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_DETAILS forIndexPath:indexPath];
    
    DoctorApplyListModel* model = self.model;
        
        
    cell.model = model;
    
        
    return cell;
    }
    
    else{
       
    DoctorAddressTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_CELLDATA forIndexPath:indexPath];
        cell.delegate = self;
        DoctorApplyListModel* model = self.model;
        cell.model = model;
       return cell;
    }
}

#pragma mark
#pragma mark - 设置组头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString* string;
    if (section == 1) {
         string = @"预约须知";
    }else
    {
        
        string = @"预约医生";
    }
    
    
    return string ;
    
    
}
#pragma mark
#pragma mark - 点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (indexPath.section == 0 && indexPath.row == 0) {
        //跳转聊天页面
        DoctorChatViewController* chatViewControll = [[DoctorChatViewController alloc] init];
        
        chatViewControll.model = self.model;
        
        [self.navigationController pushViewController:chatViewControll animated:YES];
    }
    else
    {
        DoctorAddressTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell.delegate doctorAddressTableViewCell:cell];
    }
}
//------------------------------------地图设置------------------------------------------
#pragma mark
#pragma mark - 实现代理方法
- (void)doctorAddressTableViewCell:(DoctorAddressTableViewCell *)cell {
  
 
    //是否打开地图
    UIAlertController *alerVC = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self navAppleMap];
        
        
    }];

    [alerVC addAction:action];
    
    [self presentViewController:alerVC animated:YES completion:nil];
    
    
}



//苹果地图
- (void)navAppleMap
{
    CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:self.destinationCoordinate2D];
    
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:gps addressDictionary:nil]];
    NSArray *items = @[currentLoc,toLocation];
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                          MKLaunchOptionsShowsTrafficKey : @(YES)
                          };
    
    [MKMapItem openMapsWithItems:items launchOptions:dic];
}

@end
